<?php
namespace app\common\model;

class AskAnswer extends BaseModel{
	protected $insert = ['user_id', 'ip'];

	// 用户ID修改器
	public function setUserIdAttr($user_id, $data = array()){
		$user_id = $user_id ? $user_id : $data['user_id'];

		return $user_id ? $user_id : 0;
	}
	/**
	 * 显示时间
	 * @param  string $time 创建时间
	 * @param  array  $data 数据记录
	 */
	public function getShowTimeAttr($time = '', $data = array()){
		$time = $time ? $time : $data['create_time'];
		return $time ? date('Y-m-d H:i:s', $time) : '';
	}
	/**
	 * 问题标题获取器
	 * @param  integer $ask_id 问答ID
	 * @param  array   $data   数据记录
	 */
	public function getAskTitleAttr($ask_id = 0, $data = []){
		$ask_id = $ask_id ? $ask_id : $data['ask_id'];
		$title = model('Ask')->where(['ask_id' => $ask_id])->value('title');

		return $title ? $title : '';
	}
	/**
	 * 用户名称
	 * @param  string $user_id 用户ID
	 * @param  array  $data    数据记录
	 */
	public function getUserNameAttr($user_id = '', $data = array()){
		$user_id = $user_id ? $user_id : $data['user_id'];
		$user_name = model('User')->getShowNameAttr($user_id);

		return $user_name ? $user_name : '';
	}
	public function getUserHeadUrlAttr($user_id = '', $data = array()){
		$user_id      = $user_id ? $user_id : $data['user_id'];
		$is_anonymous = $data['is_anonymous'];
		$head_url     = $is_anonymous ? model('File')->get_path('head-default.png') : model('User')->get_user_head_url($user_id);

		return $head_url ? $head_url : '';
	}
	/**
	 * 是否点赞
	 * @param  string $answer_id [description]
	 * @param  array  $data      [description]
	 * @return [type]            [description]
	 */
	public function getIsPraiseAttr($answer_id = '', $data = array()){
		$answer_id = $answer_id ? $answer_id : $data['answer_id'];
		$user_id   = $this->user_id();
		$is_praise = model('User')->is_fav($user_id, $answer_id, 'ask_answer_praise');

		return $is_praise;
	}
	/**
	 * 内容修改器
	 * @param string $content [description]
	 * @param array  $data    [description]
	 */
	public function setContentAttr($content = '', $data = array()){
		$content = $content ? $content : $data['content'];
		$content = model('File')->html_osspath_local($content);

		return $content ? $content : '';
	}
	public function getContentImgAttr($content = '', $data = array()){
		$content = $content ? $content : $data['content'];
		$content = model('File')->html_file_url_oss($content, true);

		return $content;
	}
	/**
	 * 设置最佳答案
	 * @param integer $answer_id   答案ID
	 * @param integer $ask_user_id 问题用户ID
	 */
	public function set_best($answer_id = 0, $ask_user_id = 0){
		if(!$answer_id || !$ask_user_id){
			$this->tips_info = '对不起，您的操作有误！';
			return false;
		}
		$info = $this->where(array('answer_id' => $answer_id))->find();
		if($info['ask_user_id'] != $ask_user_id){
			$this->tips_info = '对不起，您没有权限操作！';
			return false;
		}
		$ask_id         = $info['ask_id'];
		$answer_user_id = $info['user_id'];
		// 问题信息
		$ask_info = model('Ask')->where(['ask_id' => $ask_id])->find();
		
		$reward   = $ask_info['reward'];
		if($ask_info['is_set_best']){
			$this->tips_info = '此问题已设置过最佳答案了！';
			return false;
		}
		// 更新答案记录
		$this->where(['answer_id' => $answer_id])->update([
			'is_best'     => 1,
			'best_reward' => $reward ? $reward : 0,
			'best_time'   => time()
		]);
		// 更新问题记录
		model('Ask')->where(['ask_id' => $ask_id])->update([
			'is_set_best'    => 1,
			'answer_id'      => $answer_id,
			'answer_user_id' => $answer_user_id,
			'best_time'      => time(),
			'answer_time'    => time(),
		]);
		// 增加回答用户豆子
		model('UserAccount')->ask_answer($answer_user_id, $reward, $ask_id);

		return true;
	}
	/**
	 * 删除更新
	 * @param  [type] $answer_ids 回答ID
	 * @return [type]             [description]
	 */
	public function delete_update($answer_ids){
		if(!$answer_ids){
			return false;
		}
		$where['answer_id'] = array('in', $answer_ids);
		$list = $this->where($where)->field('user_id,ask_id')->select();
		foreach ($list as $rs) {
			db('Ask')->where(array('ask_id' => $rs['ask_id']))->setDec('answer_num');
			model('UserData')->update_user_data($rs['user_id'], 'answer_num', -1);
		}
		return true;
	}
	/**
	 * 还原更新
	 * @param  [type] $answer_ids 回答ID
	 * @return [type]             [description]
	 */
	public function permit_update($answer_ids){
		if(!$answer_ids){
			return false;
		}
		$where['answer_id'] = array('in', $answer_ids);
		$list = $this->where($where)->field('user_id,ask_id')->select();
		foreach ($list as $rs) {
			db('Ask')->where(array('ask_id' => $rs['ask_id']))->setInc('answer_num');
			model('UserData')->update_user_data($rs['user_id'], 'answer_num', 1);
		}
		return true;
	}
}